/**
 * Copyright (c) 2011 ~ 2013 Deepin, Inc.
 *               2011 ~ 2013 jouyouyun
 *
 * Author:      jouyouyun <jouyouwen717@gmail.com>
 * Maintainer:  jouyouyun <jouyouwen717@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 **/

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

const (
	TYPE_BAIDU  = 0
	TYPE_GOOGLE = 1

	_BAIDU_TRANSLATE_API  = "http://openapi.baidu.com/public/2.0/bmt/translate"
	_BAIDU_API_KEY        = "g9H2Uedr6fOlO2yVGWWPtRnl"
	_GOOGLE_TRANSLATE_API = "http://translate.google.cn/translate_a/t?"
)

func getPostDatas(t int32, sl, tl, data string) url.Values {
	retDatas := make(map[string][]string)

	switch t {
	case TYPE_BAIDU:
		retDatas["from"] = []string{sl}
		retDatas["to"] = []string{tl}
		retDatas["client_id"] = []string{_BAIDU_API_KEY}
		retDatas["q"] = []string{data}
	case TYPE_GOOGLE:
		retDatas["client"] = []string{"t"}
		retDatas["sl"] = []string{sl}
		retDatas["tl"] = []string{tl}
		retDatas["hl"] = []string{"zh-CN"}
		retDatas["ie"] = []string{"UTF-8"}
		retDatas["oe"] = []string{"UTF-8"}
		retDatas["q"] = []string{data}
	default:
		panic(fmt.Sprintf("Invalid type: %d\n", t))
	}

	return url.Values(retDatas)
}

func getRequest() {
	ret, err := http.Get("http://www.baidu.com")
	defer ret.Body.Close()
	if err != nil {
		panic(fmt.Sprintf("get baidu error: %s\n", err))
	}

	body, _ := ioutil.ReadAll(ret.Body)
	fmt.Printf("get baidu: %s\n", string(body))
}

/*
func postFormRequest(t int32) {
	var ret *http.Response
	var err error

	switch t {
	case TYPE_BAIDU:
		datas := getPostDatas(0, "auto", "auto", "hello")
		ret, err = http.PostForm(_BAIDU_TRANSLATE_API, datas)
	case TYPE_GOOGLE:
		datas := getPostDatas(0, "auto", "zh-CN", "hello")
		ret, err = http.PostForm(_GOOGLE_TRANSLATE_API, datas)
	default:
		panic(fmt.Sprintf("Invalid type: %d in post request\n", t))
	}
	if err != nil {
		panic(fmt.Sprintf("baidu post error: %s\n", err))
	}

	body, _ := ioutil.ReadAll(ret.Body)
	fmt.Printf("post ret: %s\n", string(body))
}
*/

func clientPostRequest(t int32, sl, tl, data string) {
	var (
		response *http.Response
		err      error
	)

	client := &http.Client{}
	datas := getPostDatas(t, sl, tl, data)
	switch t {
	case TYPE_BAIDU:
		response, err = client.PostForm(_BAIDU_TRANSLATE_API, datas)
	case TYPE_GOOGLE:
		response, err = client.PostForm(_GOOGLE_TRANSLATE_API, datas)
	default:
		panic(fmt.Sprintf("Invalid type: %d in new request\n", t))
	}
	if err != nil {
		panic(fmt.Sprintf("client post error: %s\n", err))
	}

	if response.StatusCode != 200 {
		panic(fmt.Sprintf("New Request Failed: %d\n",
			response.StatusCode))
	}

	body, _ := ioutil.ReadAll(response.Body)
	fmt.Printf("client post ret: %s\n", string(body))
}

func newRequest(t int32, sl, tl, data string) {
	var (
		request *http.Request
		err     error
	)

	switch t {
	case TYPE_BAIDU:
		request, err = http.NewRequest("POST",
			_BAIDU_TRANSLATE_API,
			nil)
	case TYPE_GOOGLE:
		request, err = http.NewRequest("POST",
			_GOOGLE_TRANSLATE_API,
			nil)
	default:
		panic(fmt.Sprintf("Invalid type: %d in new request\n", t))
	}
	if err != nil {
		panic(fmt.Sprintf("new post error: %s\n", err))
	}

	client := &http.Client{}
	datas := getPostDatas(t, sl, tl, data)
	request.Header.Add("User-Agent",
		"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53    7.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36")
	request.Header.Add("Content-Type", "application/x-www-form-urlencoded; param=value")
	request.Body = ioutil.NopCloser(strings.NewReader(datas.Encode()))
	defer request.Body.Close()

	response, err := client.Do(request)
	body, _ := ioutil.ReadAll(response.Body)
	fmt.Printf("new request ret: %s\n", string(body))
}

func main() {
	//getRequest()
        clientPostRequest(0, "auto", "auto", "hello")
        clientPostRequest(1, "auto", "zh-CN", "hello")
	//newRequest(0, "auto", "auto", "hello")
	//newRequest(1, "auto", "zh-CN", "hello")
}
